<template>
  <!-- 添加或修改所点信息对话框 -->
  <el-dialog
    title="确认收货"
    v-model="dialogVisible"
    :show-close="false"
    width="780px"
    append-to-body
  >
    <el-form
      ref="confrimReceiptRef"
      :model="form"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="备注" prop="confirmReceiptRemark">
        <el-input
          v-model="form.confirmReceiptRemark"
          type="textarea"
          :rows="2"
          placeholder="请输入"
          :max="100"
        />
      </el-form-item>

      <el-form-item label="附件" prop="confirmReceiptFile">
        <div>
          <image-upload
            title="附件"
            v-model="form.confirmReceiptFile"
            :limit="5"
          />
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { receiptOrder } from '@/api/order/order.js'

const { proxy } = getCurrentInstance();

const data = reactive({
  form: {
    id: null,
    confirmReceiptFile: null,
    confirmReceiptRemark: null,
  },
  rules: {
    confirmReceiptRemark: [
      { required: true, message: "备注不能为空", trigger: "blur" },
    ],
  },
});

const { form, rules } = toRefs(data);

const dialogVisible = ref(false);

const openDialog = async (orderId) => {
  reset();
  dialogVisible.value = true;
  form.value.id = orderId;
};

defineExpose({ openDialog });

// 取消按钮
function cancel() {
  dialogVisible.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    confirmReceiptFile: null,
    confirmReceiptRemark: null,
  };
  proxy.resetForm("confrimReceiptRef");
}

/** 提交按钮 */
const emit = defineEmits(["success"]);
function submitForm() {
  proxy.$refs["confrimReceiptRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        receiptOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("提交成功");
          reset();
          dialogVisible.value = false;
          emit("success");
        });
      } 
    }
  });
}
</script>

<style scoped lang="scss">

</style>
